package common;

import java.text.SimpleDateFormat;
import java.util.Date;

public class Utility {
	
	public static final String _ORACLE = "oracle";
	public static final String _MYSQL = "mysql";
	public static final String _SQL = "sql";
	
	/**
     * Lay ngay gio hien tai cua he thong theo dinh dang dd/MM/yyyy HH:mm:ss
     * @return
     */
    public static String getCurrentDatetime(String format) {
        java.util.Date currentDatetime = new java.util.Date();        
        if (format == null || format.length() == 0)
            format = "dd/MM/yyyy HH:mm:ss";
        return formatUtilDateAsSimpleDate(currentDatetime, format);
    }

    /**
     * Dinh dang ngay thang nam cua SQL
     * @param sqlDate
     * @param format
     * @return
     */
    public static String formatSqlAsSimpleDate(java.sql.Date sqlDate, String format) {
        String strDate = "";
    	if(sqlDate != null)
    	{
	        String defaultFormat = "dd/MM/yyyy";
	        if (format != null && format.length() > 0) {
	            defaultFormat = format;
	        }
	        java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(defaultFormat);
	        strDate = sdf.format(sqlDate.getTime());
    	}
        return strDate;
    }

    /**
     * Dinh dang ngay thang nam cua Java util
     * @param date
     * @param format
     * @return
     */
    public static String formatUtilDateAsSimpleDate(java.util.Date date, String format) {
        String defaultFormat = "dd/MM/yyyy";
        if (format != null && format.length() > 0) {
            defaultFormat = format;
        }
        java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(defaultFormat);
        return sdf.format(date);
    }

    public static Date convertStringToDate(String dateStr, String format) {
        SimpleDateFormat mySimpleDateFormat = new SimpleDateFormat(format);
        try {
            return mySimpleDateFormat.parse(dateStr);
        } catch (Exception e) {
            return null;
        }
    }

    public static String convertDateToString(Date date, String format) {
        SimpleDateFormat mySimpleDateFormat = new SimpleDateFormat(format);
        return mySimpleDateFormat.format(date);
    }
  
}
	